import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiandou/Common.dart';
import 'package:flutter/services.dart';
import 'package:xiandou/Request/DioManager.dart';
import 'package:xiandou/Request/NWApi.dart';
import 'package:xiandou/Request/NWMethod.dart';

class InviteViewScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() =>  InviteViewScreenScreenState();
}

class InviteViewScreenScreenState extends State<InviteViewScreen> {

  int  _selectCategoryIndex =0;
  List categoryList = ['邀请好友','邀请列表'];
  var currentPageValue = 0.0;
  int _page =1;
  List _inviteListData = [];
  PageController _controller = new PageController();

  @override
  void initState(){
    _requestInviteList(true);
    _controller.addListener(() {
      setState(() {
        currentPageValue = _controller.page;
        _selectCategoryIndex = _controller.page.toInt();
      });
    });
    super.initState();
  }

  void _requestInviteList(bool isRefresh) {
    var params = {'page':_page.toString()};
    DioManager().request(NWMethod.GET, NWApi.userInviteListApi,params: params,success: (data){
      setState(() {
        if(isRefresh){
          _inviteListData =data;
        }else {
          _inviteListData.addAll(data);
        }
      });
    },error: (error){
        Fluttertoast.showToast(msg: error.message);
    });
  }

  Widget categotyWidget(String label, int index) {
    return new GestureDetector(
        onTap: () {
          setState(() {
            _selectCategoryIndex = index;
            _controller.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.easeOutQuint);
          });
        },
        child: Container(
          width: MediaQuery.of(context).size.width / 2,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Text(label, style: TextStyle(
                    fontSize: 14, color: Color(0xff333333))),
              ),
              Offstage(
                  offstage: _selectCategoryIndex == index ? false : true,
                  child: Container(
                    height: 2,
                    width: 42,
                    margin: const EdgeInsets.only(top: 2),
                    decoration: new BoxDecoration(
                      color: Color(0xffFF5236),
                    ),
                  )
              )
            ],
          ),
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CupertinoNavigationBar(
          backgroundColor: Colors.white,
          middle: const Text('邀请好友得金币'),
        ),
      body: Container(
        decoration: new BoxDecoration(
          color: Color(0xffF8F8F8),
        ),
        child: Column(
          children: <Widget>[
            Container(
              height: 55,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: categoryList.length,
                itemBuilder:(BuildContext context, int index) =>categotyWidget(categoryList[index], index),
              ),
            ),
            Expanded(
              child: PageView(
                scrollDirection: Axis.horizontal,
                controller: _controller,
                  children: <Widget>[
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height-55,
                      decoration: new BoxDecoration(
//                        color: Colors.black
                        image: new DecorationImage(
                            fit: BoxFit.none,
                            image: AssetImage('assets/invite_bg@2x.png')
                        ),
                      ),
                      child: Container(
                        margin: const EdgeInsets.fromLTRB(29, 25, 29, 29),
                        padding: const EdgeInsets.only(top: 34),
                        decoration: new BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(5.0)),
                        ),
                        child: ListView(
                          children: <Widget>[
                            Column(
                              children: <Widget>[
                                Text('您的专属邀请码',style: TextStyle(fontSize: 24,fontWeight: FontWeight.bold,color: Common.blackColor),),
                                GestureDetector(
                                  child: Text('SHHHH',style: TextStyle(fontSize: 60,fontWeight: FontWeight.bold,color: Common.blackColor,fontFamily: 'Courier'),),
                                  onLongPressStart: (LongPressStartDetails details) {
                                    Clipboard.setData(ClipboardData(text: 'test test'));
                                    Fluttertoast.showToast(msg: '已经复制到粘贴板');
                                  },
                                ),
                                Text('长按复制',style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold,color: Common.grayColor),),
                              ],
                            ),
                            Container(
                              padding: const EdgeInsets.only(left: 27,right: 38),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Container(
                                    margin: const EdgeInsets.only(top: 25),
                                    child: Text('奖励规则',style: TextStyle(fontSize: 17,color: Colors.black),),
                                  ),
                                  Container(
                                    margin: const EdgeInsets.only(top: 5),
                                    height: 2,
                                    width: 70,
                                    color: Color(0xffFF5236),
                                  ),
                                  Container(
                                    padding: const EdgeInsets.only(top: 24),
                                    child: Text('1、好友接受邀请注册后，每产生一笔真实交易手续费，会产生相应比例的奖励。\n '
                                        '2、奖励将以USDT、点卡或HT的形式发放到您的交易账户，默认奖励比例为30%。\n'
                                        '3、被邀请人使用点卡抵扣手续费时，奖励将以等额的点数按比例计算后发给邀请人；被邀请人使用HT抵扣手续费时，奖励将以实际消耗的HT按比例计算后发给邀请人；'
                                        '被邀请人不使用点卡和HT抵扣的场合，奖励将以USDT进行折合计算后发给邀请人。',style: TextStyle(fontSize: 14,color: Common.blackColor),)
                                  )
                                 ]
                              ),
                            ),
                            Container(
                              margin: const EdgeInsets.only(top: 40,left: 32,right: 32,bottom: 30),
                              decoration: BoxDecoration(//背景装饰
                                  gradient: RadialGradient( //背景径向渐变
                                      colors: [Color(0xffEE5F20), Color(0xffED4327)],
                                      center: Alignment.topLeft,
                                      radius: 5
                                  ),
                                  borderRadius: new BorderRadius.circular((20.0)),
                                  boxShadow: [ //卡片阴影
                                    BoxShadow(
                                        color: Colors.black54,
                                        offset: Offset(2.0, 2.0),
                                        blurRadius: 4.0
                                    )
                                  ]
                              ),
                              child: FlatButton(
                                child: Text('分享好友得赏金',style: TextStyle(color: Colors.white),),
                              )
                            )
                          ],
                        ),
                      ),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height-55,
                        child: Container(
                          decoration: new BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.all(Radius.circular(5.0)),
                          ),
                          margin: const EdgeInsets.fromLTRB(10, 5, 10, 0),
                          child: ListView.builder(itemBuilder: (BuildContext context,int index){
                            if(_inviteListData.length == 0) {
                              return Container(
                                padding: const EdgeInsets.only(top: 50),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Image(image: AssetImage('assets/d_empty.png'),width: 200,height: 200,),
                                    SizedBox(height: 30,),
                                    Text('暂无内容',style: TextStyle(color: Common.darkColor,fontSize: 14),),
                                  ],
                                ),
                              );
                            }
                            return ListTile(
                              title: Padding(
                                padding: const EdgeInsets.only(left: 40),
                                child: Text(_inviteListData[index]['cellphone'],style: TextStyle(color: Common.blackColor,fontSize: 16,fontWeight: FontWeight.bold),),
                              ),
                              trailing: Padding(
                                padding: const EdgeInsets.only(right: 18),
                                child: Text(_inviteListData[index]['createTime'],style: TextStyle(color: Common.darkColor,fontSize: 13),),
                              )
                            );
                          },itemCount: _inviteListData.length == 0 ? 1 : _inviteListData.length,),
                        )
                    ),
                  ],
              )
            )
          ],
        ),
      ),
    );
    // TODO: implement build
    throw UnimplementedError();
  }
}